<?php

namespace support\bootstrap;

use Webman\Bootstrap;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

class Db implements Bootstrap
{
    public static function start($worker)
    {
        $config = config('database');
        
        $capsule = new Capsule;
        $capsule->addConnection($config['connections'][$config['default']]);
        
        // 设置事件调度器
        $capsule->setEventDispatcher(new Dispatcher(new Container));
        
        // 设置全局访问
        $capsule->setAsGlobal();
        
        // 启动Eloquent
        $capsule->bootEloquent();
    }
    
    public static function init()
    {
        $config = config('database');
        
        $capsule = new Capsule;
        $capsule->addConnection($config['connections'][$config['default']]);
        
        // 设置事件调度器
        $capsule->setEventDispatcher(new Dispatcher(new Container));
        
        // 设置全局访问
        $capsule->setAsGlobal();
        
        // 启动Eloquent
        $capsule->bootEloquent();
    }
    
    public static function connection()
    {
        return Capsule::connection();
    }
}
